package com.jingdianjichi.subject.domain.service.impl;

import com.alibaba.fastjson.JSON;
import com.jingdianjichi.subject.common.enums.ISDeletedFlagEnum;
import com.jingdianjichi.subject.domain.convert.SubjectCategoryConverter;
import com.jingdianjichi.subject.domain.entity.SubjectCategoryBo;
import com.jingdianjichi.subject.domain.service.SubjectCategoryDomainService;
import com.jingdianjichi.subject.infra.basic.entity.SubjectCategory;
import com.jingdianjichi.subject.infra.basic.service.SubjectCategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
@Slf4j
public class SubjectCategoryDomainServiceImpl implements SubjectCategoryDomainService {

    @Resource
    private SubjectCategoryService subjectCategoryService;

    @Override
    public void add(SubjectCategoryBo subjectCategoryBo) {
        SubjectCategory subjectCategory = SubjectCategoryConverter.INSTANCE.converBoToCategory(subjectCategoryBo);
        subjectCategory.setIsDeleted(ISDeletedFlagEnum.UN_DELETE.getCode());
        subjectCategoryService.insert(subjectCategory);
    }

    @Override
    public List<SubjectCategoryBo> queryCategory(SubjectCategoryBo subjectCategoryBo) {
        if (log.isInfoEnabled()){
            log.info("queryCategory--SubjectCategoryDomainService:{}", JSON.toJSONString(subjectCategoryBo));
        }
        try {
            SubjectCategory subjectCategory = SubjectCategoryConverter.INSTANCE
                    .converBoToCategory(subjectCategoryBo);

            subjectCategory.setIsDeleted(ISDeletedFlagEnum.UN_DELETE.getCode());
            

            List<SubjectCategory> subjectCategoryList=subjectCategoryService.queryCategory(subjectCategory);

            List<SubjectCategoryBo> subjectCategoryBoList = SubjectCategoryConverter.INSTANCE
                    .convertCategoryListToBoList(subjectCategoryList);

            return subjectCategoryBoList;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void updateCategory(SubjectCategoryBo subjectCategoryBo) {
        if (log.isInfoEnabled()){
            log.info("updateCategory--SubjectCategoryDomainService:{}", JSON.toJSONString(subjectCategoryBo));
        }
        try {
            SubjectCategory subjectCategory = SubjectCategoryConverter.INSTANCE
                    .converBoToCategory(subjectCategoryBo);

            subjectCategoryService.updateCategory(subjectCategory);
        } catch (Exception e) {
            log.info("Massenge---updateCategory--SubjectCategoryDomainService:{}",e.getMessage());
            throw new RuntimeException(e);
        }
    }

    @Override
    public void deleteCategory(SubjectCategoryBo subjectCategoryBo) {
        if (log.isInfoEnabled()){
            log.info("deleteCategory--SubjectCategoryDomainService:{}", JSON.toJSONString(subjectCategoryBo));
        }
        try {
            SubjectCategory subjectCategory = SubjectCategoryConverter.INSTANCE
                    .converBoToCategory(subjectCategoryBo);
            subjectCategoryService.deleteCategory(subjectCategory);
        } catch (Exception e) {
            log.info("Massenge---deleteCategory--SubjectCategoryDomainService:{}",e.getMessage());
            throw new RuntimeException(e);
        }
    }
}
